package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizAgreement;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.service.ISysBizAgreementService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【协议】控制器
 *
 * @author wangxin
 * @version 1.0
 * @date 2024-01-03
 **/
@RestController
@RequestMapping("/shop/agreement")
public class SysBizAgreementController extends BaseController {
    @Resource
    private ISysBizAgreementService sysBizAgreementService;

    /**
     * 查询协议列表
     */
    @PreAuthorize("@ss.hasPermi('shop:agreement:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizAgreement sysBizAgreement) {
        startPage();
//        setTenantId(sysBizAgreement);
        List<SysBizAgreement> list = sysBizAgreementService.selectSysBizAgreementList(sysBizAgreement);
        return getDataTable(list);
    }

    /**
     * 查询协议列表(查询关联的商品表)
     */
    @PreAuthorize("@ss.hasPermi('shop:agreement:list')")
    @GetMapping("/listGoods")
    public TableDataInfo listGoods(SysBizAgreement sysBizAgreement) {
        startPage();
//        setTenantId(sysBizAgreement);
        List<SysBizAgreement> list = sysBizAgreementService.selectSysBizAgreementList(sysBizAgreement);
        return getDataTable(list);
    }

    private void setTenantId(SysBizAgreement sysBizAgreement) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizAgreement.setTenantId(tenantId);
        }
    }

    /**
     * 导出协议列表
     */
    @PreAuthorize("@ss.hasPermi('shop:agreement:export')")
    @Log(title = "协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizAgreement sysBizAgreement) {
//        setTenantId(sysBizAgreement);
        List<SysBizAgreement> list = sysBizAgreementService.selectSysBizAgreementList(sysBizAgreement);
        ExcelUtil<SysBizAgreement> util = new ExcelUtil<SysBizAgreement>(SysBizAgreement.class);
        util.exportExcel(response, list, "协议数据");
    }

    /**
     * 获取协议详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:agreement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizAgreementService.selectSysBizAgreementById(id));
    }

    /**
     * 新增协议
     */
    @PreAuthorize("@ss.hasPermi('shop:agreement:add')")
    @Log(title = "协议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizAgreement sysBizAgreement) {
        return toAjax(sysBizAgreementService.insertSysBizAgreement(sysBizAgreement));
    }

    /**
     * 修改协议
     */
    @PreAuthorize("@ss.hasPermi('shop:agreement:edit')")
    @Log(title = "协议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizAgreement sysBizAgreement) {
        return toAjax(sysBizAgreementService.updateSysBizAgreement(sysBizAgreement));
    }

    /**
     * 删除协议
     */
    @PreAuthorize("@ss.hasPermi('shop:agreement:remove')")
    @Log(title = "协议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizAgreementService.deleteSysBizAgreementByIds(ids));
    }


}
